if exists (select 1
   from dbo.sysreferences r join dbo.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('Admin_Roles_Func') and o.name = 'FK_ADMIN_RO_REFERENCE_ADMIN_US')
alter table Admin_Roles_Func
   drop constraint FK_ADMIN_RO_REFERENCE_ADMIN_US
go

if exists (select 1
   from dbo.sysreferences r join dbo.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('Admin_Roles_Func') and o.name = 'FK_ADMIN_RO_REFERENCE_ADMIN_RO')
alter table Admin_Roles_Func
   drop constraint FK_ADMIN_RO_REFERENCE_ADMIN_RO
go

if exists (select 1
            from  sysobjects
           where  id = object_id('Admin_Roles_Func')
            and   type = 'U')
   drop table Admin_Roles_Func
go

/*==============================================================*/
/* Table: Admin_Roles_Func                                      */
/*==============================================================*/
create table Admin_Roles_Func (
   TemplateId           char(36)             not null,
   FuncCode             varchar(20)          not null,
   CreateId             char(36)             null,
   CreateDate           Date             null,
   UpdateId             char(36)             null,
   UpdateDate           Date             null,
   RowVerNo             timestamp            null,
   constraint PK_ADMIN_ROLES_FUNC primary key (TemplateId, FuncCode)
)
go

alter table Admin_Roles_Func
   add constraint FK_ADMIN_RO_REFERENCE_ADMIN_US foreign key (TemplateId)
      references Admin_User_Roles_Template (TemplateId)
go

alter table Admin_Roles_Func
   add constraint FK_ADMIN_RO_REFERENCE_ADMIN_RO foreign key (FuncCode)
      references Admin_Roles_Func_Template (FuncCode)
go
